package com.school.service.impl;

import com.school.entity.PublicityTime;
import com.school.repository.PublicityTimeRepository;
import com.school.service.PublicityTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PublicityTimeServiceImp implements PublicityTimeService {
    @Autowired
    PublicityTimeRepository publicityTimeRepository;

    /**
     * 增加
     * @param publicityTime
     * @return
     */
    @Override
    public PublicityTime addPublicityTime(PublicityTime publicityTime) {
        publicityTime.setCreateDate(new Date());
        publicityTime.setUpdateDate(new Date());
        return publicityTimeRepository.saveAndFlush(publicityTime);
    }

    /**
     * 修改
     * @param publicityTime
     * @return
     */
    @Override
    public PublicityTime updatePublicityTime(PublicityTime publicityTime) {
        publicityTime.setUpdateDate(new Date());
        return publicityTimeRepository.saveAndFlush(publicityTime);
    }

    /**
     * 查看详情
     * @param id
     */
    @Override
    public PublicityTime getDetailsById(String id) {
        return publicityTimeRepository.findById(Integer.valueOf(id)).get();
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deletePublicityTime(String id) {
        PublicityTime publicityTime;
        try {
            publicityTime = publicityTimeRepository.findById(Integer.valueOf(id)).get();
            publicityTimeRepository.delete(publicityTime);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

    /**
     * 批量删除
     * @param idList
     */
    @Override
    public void deletePublicityTimeList(String idList) {
        String[] split = idList.split(",");
        List<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add(Integer.parseInt(s));
        }
        publicityTimeRepository.deletePublicityTimesByIdIn(list);
    }

    /**
     * 根据条件分页查询
     * @param publicityTime,page,size
     */
    @Override
    public Page<PublicityTime> specificPageQueryPublicityTimeList(PublicityTime publicityTime, int page, int size) {
        Specification querySpecification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(publicityTime != null)
                {
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        PageRequest pageRequest = PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC,"updateDate"));
        return publicityTimeRepository.findAll(querySpecification,pageRequest);
    }

}
